<?php

include_once ('bn_path.php');

class bn_comment
{
    private $dl;
    private $gf;
    protected static $instance;

    function __construct()
    {
        $this->dl = new bn_dal;
        $this->gf = new bn_genralfunc;
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function insert_comment($pid, $name, $mail, $comment)
    {
        $pd = new bn_parsidate;
        return $this->dl->insert('comments', array($pid,$name,$this->gf->get_ip(),$comment,$mail,$this->gf->date(),'enable'));
    }

    public function delete($id)
    {
        return $this->dl->delete('comments', $id);
    }

    public function update($col, $val, $id)
    {
        return $this->dl->update('comments', array($col => $val), array('id=' => $id));
    }

    public function select_postcomments($pid)
    {
        return $this->dl->Select("select * from comments where pid=? and status='enable'",
            $pid);
    }

    public function select_comments($col = '', $val = '')
    {
        if (empty($col) || empty($val))
            return $this->dl->Select('SELECT c.id,name,pid,title,ip,mail,c.date,c.comment,c.status from comments c join posts p on pid=p.id');
        else
            return $this->dl->Select("SELECT c.id,name,pid,title,ip,mail,c.date,c.comment,c.status from comments c join posts p on pid=p.id and c.$col=?",$val);
    }


    function __destruct()
    {
        unset($this->dl);
    }
}

?>